package de.hsb.ai.learning;

import weka.classifiers.Classifier;
import weka.core.Instances;
import de.hsb.ai.agent.analyzer.FugitivePath;
import de.hsb.ai.agent.analyzer.FugitivePathWeighterFactory;
import de.hsb.ai.learning.FugitiveAnalyzer.TransitionBuilder;
import de.hsb.ai.util.map.ScotlandJadeMapHelper.ScotlandJadeEdge;

public class ClassificationWeighterFactory implements FugitivePathWeighterFactory {
	
	protected FugitiveAnalyzer analyzer;
	
	protected Classifier classifier;
	
	protected Instances dataSet = Transition.createInstances("cwf");
	
	public ClassificationWeighterFactory(FugitiveAnalyzer analyzer, Classifier classifier) {
		this.analyzer = analyzer;
		this.classifier = classifier;
	}
	
	@Override
	public Weighter createWeighter(FugitivePath path) {
		return new ClassificationWeighter(path);
	}
	
	class ClassificationWeighter implements Weighter {
		
		private final TransitionBuilder builder;

		public ClassificationWeighter(FugitivePath path) {
			builder = analyzer.getTransitionBuilder(path.getEndVertex(), path.getMoveNumber(), path.getPreviousTransport());
		}

		@Override
		public double getWeight(ScotlandJadeEdge e) throws Exception {
			Transition transition = builder.getTransition(e, false);
			transition.setDataset(dataSet);
			double[] dist = classifier.distributionForInstance(transition);
			return dist[State.BOOLEAN_VALUES_TRUE_INDEX];
		}
		
	}

}
